package com.gwz.sys.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.gwz.sys.constast.SYSConstast;
import com.gwz.sys.domain.User;
import com.gwz.sys.service.UserService;
import com.gwz.sys.utils.DataGridView;
import com.gwz.sys.utils.HanyuPinyinHelperUtils;
import com.gwz.sys.utils.MD5Utils;
import com.gwz.sys.utils.ResultObj;
import com.gwz.sys.vo.UserVo;

@RestController
@RequestMapping("user")
public class UserController {
	
	@Autowired
	private UserService userService;
	
	@RequestMapping("loadAllUser")
	public DataGridView loadAllUser(UserVo userVo) {
		return userService.queryAllUser(userVo);
	}
	
	@RequestMapping("loadMaxOrderNumber")
	public Integer loadMaxOrderNumber() {
		return userService.queryMaxOrderNumber()+1;
	}
	
	@RequestMapping("loadUserByDeptId")
	public List<User> loadUserByDeptId(UserVo userVo){
		userVo.setAvailable(SYSConstast.SYS_AVAILABLE_TRUE);
		return userService.queryUserByDeptId(userVo);
	}
	
	@RequestMapping("changeUserNameToPinyin")
	public String changeUserNameToPinyin(UserVo userVo) {
		String userName = userVo.getName();
		return HanyuPinyinHelperUtils.toHanyuPinyin(userName);
	}
	
	@RequestMapping("addUser")
	public ResultObj addUser(UserVo userVo) {
		userVo.setType(SYSConstast.USER_TYPE_NORMAL);
		userVo.setSalt(MD5Utils.createSalt());
		userVo.setPwd(MD5Utils.md5(SYSConstast.USER_PWD_DEFAULT, userVo.getSalt(), 2));
		userVo.setImgpath(SYSConstast.USER_DEFAULT_IMGTITLE);
		ResultObj resultObj = new ResultObj(200, "添加成功");
		try {
			userService.addUser(userVo);
			return resultObj;
		} catch (Exception e) {
			resultObj.setCode(-1);
			resultObj.setMsg("添加失败");
			e.printStackTrace();
			return resultObj;
		}
	}
	
	@RequestMapping("loadUserById")
	public User loadUserById(UserVo userVo) {
		return userService.queryUserById(userVo.getId());
	}
	
	@RequestMapping("deleteUser")
	public ResultObj deleteUser(UserVo userVo) {
		ResultObj resultObj = new ResultObj(200, "删除成功");
		try {
			userService.deleteUser(userVo);
			return resultObj;
		} catch (Exception e) {
			resultObj.setCode(-1);
			resultObj.setMsg("删除失败");
			e.printStackTrace();
			return resultObj;
		}
	}
	
	@RequestMapping("resetUserpwd")
	public ResultObj resetUserpwd(UserVo userVo) {
		ResultObj resultObj = new ResultObj(200, "重置成功");
		try {
			userService.resetUserpwd(userVo);
			return resultObj;
		} catch (Exception e) {
			resultObj.setCode(-1);
			resultObj.setMsg("重置失败");
			e.printStackTrace();
			return resultObj;
		}
	}
	
	@RequestMapping("loadUserRole")
	public DataGridView loadUserRole(UserVo userVo) {
		return userService.queryUserRole(userVo);
	}
	
	@RequestMapping("saveUserRole")
	public ResultObj saveUserRole(UserVo userVo) {
		ResultObj resultObj = new ResultObj(200, "分配成功");
		try {
			userService.saveUserRole(userVo);
			return resultObj;
		} catch (Exception e) {
			resultObj.setCode(-1);
			resultObj.setMsg("分配败");
			e.printStackTrace();
			return resultObj;
		}
	}
}
